package com.example.filetransfer.gui;

import com.example.filetransfer.server.FileTransferServer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ServerGUI extends JFrame {
    private FileTransferServer server;
    private JTextArea logArea;
    private JTextField portField;
    private JButton startButton;
    private JButton stopButton;
    private JLabel statusLabel;

    public ServerGUI() {
        setTitle("文件传输服务器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 400);
        setLocationRelativeTo(null);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 创建顶部控制面板
        JPanel controlPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        // 端口输入
        gbc.gridx = 0;
        gbc.gridy = 0;
        controlPanel.add(new JLabel("监听端口:"), gbc);

        gbc.gridx = 1;
        portField = new JTextField("9999", 10);
        controlPanel.add(portField, gbc);

        // 按钮
        gbc.gridx = 2;
        startButton = new JButton("启动服务器");
        controlPanel.add(startButton, gbc);

        gbc.gridx = 3;
        stopButton = new JButton("停止服务器");
        stopButton.setEnabled(false);
        controlPanel.add(stopButton, gbc);

        // 状态标签
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 4;
        statusLabel = new JLabel("服务器状态: 未启动");
        controlPanel.add(statusLabel, gbc);

        mainPanel.add(controlPanel, BorderLayout.NORTH);

        // 创建日志区域
        logArea = new JTextArea();
        logArea.setEditable(false);
        logArea.setLineWrap(true);
        logArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(logArea);
        mainPanel.add(scrollPane, BorderLayout.CENTER);

        // 设置事件处理
        startButton.addActionListener(e -> startServer());
        stopButton.addActionListener(e -> stopServer());

        // 窗口关闭事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                stopServer();
            }
        });

        add(mainPanel);
    }

    private void startServer() {
        try {
            int port = Integer.parseInt(portField.getText());
            server = new FileTransferServer(port);
            server.setMessageHandler(this::logMessage);
            server.start();

            startButton.setEnabled(false);
            stopButton.setEnabled(true);
            portField.setEnabled(false);
            statusLabel.setText("服务器状态: 运行中");
            logMessage("服务器已启动，监听端口: " + port);
        } catch (NumberFormatException e) {
            showError("端口号必须是有效的数字");
        } catch (Exception e) {
            showError("启动服务器失败: " + e.getMessage());
        }
    }

    private void stopServer() {
        if (server != null) {
            server.stop();
            startButton.setEnabled(true);
            stopButton.setEnabled(false);
            portField.setEnabled(true);
            statusLabel.setText("服务器状态: 已停止");
            logMessage("服务器已停止");
        }
    }

    private void logMessage(String message) {
        SwingUtilities.invokeLater(() -> {
            logArea.append(message + "\n");
            logArea.setCaretPosition(logArea.getDocument().getLength());
        });
    }

    private void showError(String message) {
        SwingUtilities.invokeLater(() -> {
            JOptionPane.showMessageDialog(this,
                    message,
                    "错误",
                    JOptionPane.ERROR_MESSAGE);
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ServerGUI gui = new ServerGUI();
            gui.setVisible(true);
        });
    }
} 